/** 
 *
 * Lisense:
 * Copyright (c) 2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @fileDesc:TODO
 * @author:dairymix
 * @version:1.0.0
 */
package cn.wanghaomiao.seimi.http;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory;

/** 
 * @ClassName:SeimiCookieSpec
 * @Description:TODO
 * @author:dairymix
 * @version:1.0.0
 */
public class SeimiCookieSpec {

    public SeimiCookieSpec() {
        regCookieSpec();
    }

    public void regCookieSpec() {
        setEasySpecProvider(getEasySpecProvider());
        setReg(getReg());
        setRequestConfig(getRequestConfig());
        setHttpclient(getHttpclient());
    }

    CookieSpecProvider easySpecProvider;
    Registry<CookieSpecProvider> reg;
    RequestConfig requestConfig;
    CloseableHttpClient httpclient;
    String specName = "mySpec";

    public CookieSpecProvider getEasySpecProvider() {
        if (easySpecProvider == null) {
            return new SeimiCookieSpecProvider();
        }
        return easySpecProvider;
    }

    public void setEasySpecProvider(CookieSpecProvider easySpecProvider) {
        this.easySpecProvider = easySpecProvider;
    }

    public Registry<CookieSpecProvider> getReg() {
        if (reg == null) {
            return RegistryBuilder
                    .<CookieSpecProvider> create()
                    .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
                    .register(CookieSpecs.BROWSER_COMPATIBILITY,new BrowserCompatSpecFactory())
                    .register(getSpecName(), getEasySpecProvider())
                    .build();
        }
        return reg;
    }

    public void setReg(Registry<CookieSpecProvider> reg) {
        this.reg = reg;
    }

    public RequestConfig getRequestConfig() {
        if (requestConfig == null) {
            return RequestConfig
                    .custom()
                    .setCookieSpec(getSpecName())
                    .build();
        }
        return requestConfig;
    }

    public void setRequestConfig(RequestConfig requestConfig) {
        this.requestConfig = requestConfig;
    }

    public CloseableHttpClient getHttpclient() {
        if (httpclient == null) {
            return HttpClients
                    .custom()
                    .setDefaultCookieSpecRegistry(getReg())
                    .setDefaultRequestConfig(getRequestConfig())
                    .build();
        }
        return httpclient;
    }

    public void setHttpclient(CloseableHttpClient httpclient) {
        this.httpclient = httpclient;
    }

    public String getSpecName() {
        return specName;
    }

    public void setSpecName(String specName) {
        this.specName = specName;
    }

}
